﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Survey.Models;

namespace Survey.ViewModels
{
    /// <summary>
    /// A ViewModel to use with the SurveyView and SurveyModel. This class contains the logic relating to the View,
    /// what should be shown, it's style and any other information which the View can use.
    /// </summary>
    public class BasicViewModel : ISurveyViewModel
    {
        private ISurveyRetrieverModel _surveyRetrieverModel;
        private SurveyModel _survey;

        /// <summary>
        /// Default Constructor
        /// </summary>
        public BasicViewModel()
        {
            _surveyRetrieverModel = new XmlSurveyRetrieverModel();

            _surveyRetrieverModel.PropertyChanged += new PropertyChangedEventHandler(surveyModel_PropertyChanged);

            _surveyRetrieverModel.PopulateSurvey();
        }

        /// <summary>
        /// Handles properties changing in the Model
        /// </summary>
        private void surveyModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            string propertyName = e.PropertyName;

            switch(propertyName)
            {
                case "Survey":
                    Survey = _surveyRetrieverModel.Survey;
                    break;
            }
        }

        /// <inheritdoc />
        public SurveyModel Survey
        {
            get
            {
                return _survey;
            }
            set
            {
                //Check the reference
                if (_survey == value) return;

                _survey = value;
                OnPropertyChanged("Survey");
            }
        }

        /// <inheritdoc />
        public void VoteForOption(string option)
        {
            throw new NotImplementedException();
        }

        #region INotifyPropertyChanged Members

        private void OnPropertyChanged(string propertyName)
        {

            if (PropertyChanged != null)

                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));

        }

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
}
